// pages/change_info/change_info.js
var qiniuUploader = require("../qiniuUploader.js");
var uploadimage = function (res) {

  for (var i = 0; i < res.length; i++) {

    qiniuUploader.upload(res[i], (res) => {
      console.log("上传成功")
    },
      (error) => {
        console.log("上传失败")
      },
      {
        uploadURL: 'https://up.qiniup.com',
        region: 'ECN',

        domain: 'www.liuxuanlong.club',
        uptokenURL: 'https://www.liuxuanlong.xyz/fenda/wx/upToken'
      }
    );

  }
}
var wxNum;
var info={
  headImg_src:[],
  name:"",
  sex:"男",
  goodAt:"",
  motto:"健康第一",
  hospital:"",
  pwd:""
};
Page({

  /**
   * 页面的初始数据
   */
  data: {
      headImg_src:"../image/dian.jpg",
      sex:"男",
      hospital:'烟台市人民医院',
      goodAt:'情感',
      motto:'天道酬勤',
      name:'王尼玛',
      loading: "display",
      font: "none"
  },
  submit:function(e){
    var that=this;
   console.log(e.detail.value);
   var datas=e.detail.value;
   info.name=datas.name;
   info.sex=datas.sex;
   info.hospital=datas.hospital;
   info.motto=datas.motto;
   info.goodAt=datas.goodAt;
   info.pwd=datas.pwd;
   uploadimage(info.headImg_src);
   wx.request({
     url: 'https://www.liuxuanlong.xyz/fenda/wx/updateMasterInfo',
     data:{
      wxNum:wxNum,
      headImg:info.headImg_src[0],
      name:info.name,
      goodAt:info.goodAt,
      info:info.motto,
      password:info.pwd
      
       
     },
     success:function(){
       
       wx.showToast({
         title: '修改成功！',
         icon:'success',
         duration:500
       })
        setTimeout(function(){
          wx.switchTab({
            url: '../info/info',
          })
        },500);
     },
   })
  },
  change_sex:function(){
    
    if(this.data.sex=='男')
    {
      this.setData(
        {
          sex:"女"
        }
      );
      
    }
    else{
      this.setData(
        {
          sex:"男"
        }
      );
    }
    
  },
  change_headImg:function(){
    var that=this;
    wx.chooseImage({
      count:1,
      success: function(res) {
        that.setData(
          {
            headImg_src:res.tempFilePaths[0]
          }
        );
        info.headImg_src=res.tempFilePaths;
       
      },
    })    
  },
  load_Info: function () {
    var that = this;
    wxNum = wx.getStorageSync('login_uuId');
    console.log(wxNum, '缓存uuid')
    wx.request({
      url: 'https://www.liuxuanlong.xyz/fenda/wx/findMasterInfo',
      data: {
        wxNum: wxNum
      },
      success: function (res) {
        console.log(res.data, '我的信息');

        var datas = res.data;

        if (datas.sex == 'man') datas.sex = '../image/man.png'; //判断 性别 给图标
        else datas.sex = '../image/woman.png';
        that.setData(
          {
            headImg_src: datas.headImg,
            name: datas.name,
            hospital:datas.hospital.name,
            //sex:datas.      性别
            //address:datas. 
            goodAt: datas.goodAt,
            pwd:datas.password,
            motto:datas.info,    
            loading: "none",
            font: "display",
            

          }
        );


      },
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
        
        this.load_Info();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})